// pages/commentDetails/commentDetails.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    comment_img:[],
    like:false,
    id:'',
    userTelphone:'',
    commentDetails:[],
    likeNumber : [],
    LookUserTelphone:'',
    username:'',
    headImage:'',
    storeName:'',
    storeDetails:{}
  },
  // 查看图片
  clickImg(){
    // 定义临时数组
    let nowArr = []
    if(this.data.commentDetails.imgPathID.length > 1){
      for(let i of this.data.commentDetails.imgPathID){
        nowArr.push(i)
      }
      wx.previewImage({
        urls: nowArr
      })
    }else{
      wx.previewImage({
        urls: this.data.commentDetails.imgPathID
      })
    }
  },
  // 获取图片
  // getcommentDetailsImg(){
  //   let comment_imgID = this.data.commentDetails.imgPathID
  //   let comment_img = []
  //   if(comment_imgID.length == 1){
  //     wx.cloud.downloadFile({
  //       fileID : comment_imgID[0],
  //       success:(res)=>{
  //         comment_img.push(res.tempFilePath)
  //         this.setData({
  //           comment_img : comment_img
  //         })
  //       }
  //     })
  //   }else if(comment_imgID.length > 1){
  //     for(let i =0;i<=comment_imgID.length;i++){
  //       wx.cloud.downloadFile({
  //         fileID : comment_imgID[i],
  //         success:(res)=>{
  //           comment_img[i] = [{src : res.tempFilePath}]
  //         }
  //       })
  //     }
  //     setTimeout(()=>{
  //       let nowArr = []
  //       for(let i in comment_img){
  //         nowArr.push(comment_img[i][0])
  //       }
  //       this.setData({
  //         comment_img : nowArr
  //       })
  //     },1200)
  //   }
  // },
  getcommentDetails(){
    // 获取页面数据
    const db = wx.cloud.database()
    db.collection('CR_comments').where({
      _id:this.data.id
    }).get({
      success:(res)=>{  
        this.setData({
          commentDetails : res.data[0]
        })
      }
    })
  },
    // 点赞事件监听
    clickLike(){
      if(this.data.LookUserTelphone == this.data.userTelphone){
        wx.showToast({
          title: '你不可以为自己的评论点赞！',
          icon: 'none'
        })
        return
      }
      const db1 = wx.cloud.database()
      const that = this
      // 判断数组内是否存在浏览者
      if(this.data.likeNumber.indexOf(this.data.LookUserTelphone) == -1){
        let nowArr = that.data.likeNumber
        // 添加数组内的浏览者电话号码
        nowArr.push(that.data.LookUserTelphone)
        db1.collection('CR_comments').doc(that.data.id).update({
          data : {
            likeNumber : nowArr
          },success:(res)=>{
              //  获取最新点赞数据
              this.getcommentDetails()    // 获取点赞数组
              this.setData({
                likeNumber : this.data.commentDetails.likeNumber
              })
              if(this.data.likeNumber.indexOf(this.data.LookUserTelphone) != -1){
                this.setData({
                  like : true
                })
              }
          }
        })
      }else{
        wx.showModal({
          cancelColor: '#ccc',
          cancelText: '取消',
          confirmColor: 'red',
          confirmText: '确定',
          showCancel: true,
          title: '是否取消点赞?',
          success: (result) => {
            if(result.confirm){
              let nowArr = that.data.likeNumber

              // 删除数组内的浏览者电话号码
              nowArr.splice(nowArr.indexOf(that.data.LookUserTelphone),1)
              db1.collection('CR_comments').doc(that.data.id).update({
                data : {
                  likeNumber : nowArr
                },success:(res)=>{
                    //  获取最新点赞数据
                    this.getcommentDetails()
                        // 获取点赞数组
                    this.setData({
                      likeNumber : nowArr,
                      like : false
                    })
                }
              })
            }
          }
        })
      }
    },

    yunGetStoreName(foodId){
      let db = wx.cloud.database()
      let taht = this
      db.collection('CR_foods')
      .where({_id:foodId})
      .get({
        success:(res)=>{
          taht.setData({
            storeName:res.data[0].store
          })
          setTimeout((res)=>{
            let storeName = this.data.storeName
            this.yunGetStoreDetail(storeName)
          },800)
        }
      })
    },

    yunGetStoreDetail(storeName){
      let db = wx.cloud.database()
      let taht = this
      db.collection('CR_stores')
      .where({storeName:storeName})
      .get({
        success:(res)=>{
          taht.setData({
            storeDetails:res.data[0]
          })
          wx.hideLoading({
            
          })
        }
      })
    },

    toStoreDetail(){
      wx.navigateTo({
        url: '/pages/stores/stores?storeName='+this.data.storeName
      })
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id:options.id,
      username:options.username,
      headImage:options.headImage,
      userTelphone:options.userTelphone,
    })
    wx.getStorage({
      key:'Tel',
      success:(res)=>{
        this.setData({
          LookUserTelphone:res.data
        })
      }
    })
    
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.showLoading({
      title: '加载中...'
    })
    // 获取页面的数据
    this.getcommentDetails()
    setTimeout(()=>{
      // 获取店铺名称
      this.yunGetStoreName(this.data.commentDetails.belong[0])
      // 获取点赞数组
      this.setData({
        likeNumber : this.data.commentDetails.likeNumber
      })
      if(this.data.likeNumber.indexOf(this.data.LookUserTelphone) != -1){
        this.setData({
          like : true
        })
      }
    },1000)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // 清除加载中
    wx.hideLoading({
      
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})